<template>
  <div @change="transferValue()">
    <el-tag>材料</el-tag>
    <el-checkbox @change="clickToChange()">需要</el-checkbox>
    <br/>
    <span>数量:</span>
    <el-input v-model="cai_liao.shu_liang" :disabled="disable"
                placeholder="请输入数量" size="small" class="length"></el-input>
    <span>损耗数量:</span>
    <el-input v-model="cai_liao.sun_hao_shu_liang" :disabled="disable"
                placeholder="输入损耗数" size="small" class = "length"></el-input>
    <br/>
    <span>尺寸(单位：毫米):</span>
    <el-input v-model="cai_liao.keng_gao" :disabled="disable"
                placeholder="请输入坑高" size="small" class = "length"></el-input>
    <el-input v-model="cai_liao.kuan" :disabled="disable"
                placeholder="请输入宽" size="small" class = "length"></el-input>
    <br/>
    <span>种类:</span>
    <el-select v-model="cai_liao.zhong_lei" placeholder="请选择" class="length" size="small" :disabled="disable" @change="clearInfo()">
      <el-option v-for="item in option.zhong_lei" :key="item" :label="item" :value="item"></el-option>
    </el-select>
    <br/>
    <span>克重(单位：克):</span>
    <el-select v-model="cai_liao.ke_zhong" placeholder="请选择" size="small" class="length" :disabled="disable" @change="getPrice()">
      <el-option v-for="item in option.ke_zhong[name]" :key="item" :label="item" :value="item"></el-option>
    </el-select>
    <input v-model.lazy="cai_liao.ke_zhong" placeholder="请输入克重" class="length"
           v-if="cai_liao.ke_zhong ==='自填'"/>
    <span>单价(单位：元/吨):</span>
    <el-select v-model="cai_liao.dan_jia" placeholder="请选择" class="length" size="small" :disabled="disable">
      <el-option v-for="item in option.dan_jia[name]" :key="item" :label="item" :value="item"></el-option>
    </el-select>
    <input v-model.lazy="cai_liao.dan_jia" placeholder="请输入单价"
           v-if="cai_liao.dan_jia ==='自填'" class="length"/>
    <div>
      <el-tag class="content" type="success">分切材料选择坑高:{{computeApp()}}</el-tag>
      <el-tag type="warning" class="cost">成本: {{computeCost()}}</el-tag>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Cailiao',
  props: ['confirm'],
  mounted () {
    this.getFenQie()
  },
  data () {
    return {
      disable: true,
      cai_liao: {
        ke_zhong: '',
        dan_jia: '',
        zhong_lei: '',
        keng_gao: '',
        kuan: '',
        shu_liang: '',
        sun_hao_shu_liang: ''
      },
      option: {
        ke_zhong: {
          baiban: [230, 250, 300, 350, 400, '自填'],
          baika: [250, 300, 350, 400, '自填'],
          shuangtong: [105, 128, 157, 200, 250, 300, 350, 400, '自填']
        },
        dan_jia: {
          baiban: [5000, 4900, 4800, 4700, 4600, '自填'],
          baika: [5300, 5200, 5100, 5000, '自填'],
          shuangtong: [6000, 5900, 5800, '自填']
        },
        zhong_lei: ['双铜', '白卡', '白板', '双白', '银卡', '金卡'],
        fen_qie: {
          baiban: [635, 661, 686, 711, 737, 762, 787, 813, 838, 864, 889, 914, 940, 965, 991, 1016, 1042, 1068,
            1194, 1219, 1245, 1270, 1295],
          baika: {
            250: [787, 840, 889, 1092, 1194],
            300: [661, 700, 720, 787, 889, 1000, 1092, 1194, 686, 840, 940],
            350: [635, 661, 686, 700, 720, 740, 787, 840, 889, 1000, 1092, 1194],
            400: [635, 661, 710, 740, 787, 840, 889, 1000, 1092, 1194]
          },
          shuangtong: [787, 889, 1092, 1194]
        }
      }
    }
  },
  watch: {
    confirm (nV, oV) {
      if (nV !== oV) this.clickToFin()
    }
  },
  computed: {
    name () {
      let pin = ['shuangtong', 'baika', 'baiban', 'shuangbai', 'yinka', 'jinka']
      let i = this.option.zhong_lei.indexOf(this.cai_liao.zhong_lei)
      return i === -1 ? '' : pin[i]
    }
  },
  methods: {
    transferValue () {
      console.log('ffff', this.computeApp())
      let n = Math.floor(this.computeApp() / parseInt(this.cai_liao.keng_gao))
      let x = Math.floor(this.computeApp() / n + 0.5)
      let Q = parseInt(this.cai_liao.shu_liang) + parseInt(this.cai_liao.sun_hao_shu_liang)
      this.$emit('transfer', x, parseInt(this.cai_liao.kuan), Q, parseInt(this.cai_liao.ke_zhong))
    },
    computeApp () {
      let x = []
      let keZ = parseInt(this.cai_liao.ke_zhong)
      let fenQie = [0]
      if (this.cai_liao.zhong_lei === '白卡' && this.cai_liao.ke_zhong) fenQie = this.option.fen_qie.baika[keZ]
      if (this.cai_liao.zhong_lei === '双铜') fenQie = this.option.fen_qie.shuangtong
      if (this.cai_liao.zhong_lei === '白板') fenQie = this.option.fen_qie.baiban
      for (let i = 0; i < fenQie.length; i++) {
        let m = Math.floor(fenQie[i] / parseInt(this.cai_liao.keng_gao))
        if (m < 1) x.push(999)
        else x.push(fenQie[i] / m - parseInt(this.cai_liao.keng_gao))
      }
      return fenQie[x.indexOf(Math.min.apply(Math, x))]
    },
    computeCost () {
      let n = Math.floor(this.computeApp() / parseInt(this.cai_liao.keng_gao))
      let x = Math.floor(this.computeApp() / n + 0.5)
      let y = parseInt(this.cai_liao.kuan)
      let q = parseInt(this.cai_liao.shu_liang) + parseInt(this.cai_liao.sun_hao_shu_liang)
      let price = parseInt(this.cai_liao.dan_jia)
      let ke = parseInt(this.cai_liao.ke_zhong)
      let cost = 0
      let costStr = ''
      if (x < y) {
        cost = (x + 15) * (y + 10) * q * price * ke / Math.pow(10, 12)
        if (cost <= 1300) {
          costStr = `(${x}+15) × (${y}+10) × ${q} × ${price} × ${ke} × (10^-12) + 30 = ${cost + 30}`
        } else costStr = `(${x}+15) × (${y}+10) × ${q} × ${price} × ${ke} × (10^-12) = ${cost}`
      } else {
        cost = (x + 10) * (y + 15) * q * price * ke / Math.pow(10, 12)
        if (cost <= 1300) {
          costStr = `(${x}+10) × (${y}+15) × ${q} × ${price} × ${ke} × (10^-12) + 30 = ${cost + 30}`
        } else costStr = `(${x}+10) × (${y}+15) × ${q} × ${price} × ${ke} × (10^-12) = ${cost}`
      }
      return costStr
    },
    clickToFin () {
      if (!this.disable) this.$emit('transferToFather', 'cailiao', this.cai_liao)
    },
    clickToChange () {
      this.disable = !this.disable
    },
    async getFenQie () {
      const res = await this.$http.post('/costOfferTemplateValue/getTemplateValue', {templateId: 2})
      this.option.fen_qie = JSON.parse(res.data.info.templateValue.templateValue)
    },
    getPrice () {
      if (this.cai_liao.zhong_lei === '双铜') {
        switch (this.cai_liao.ke_zhong) {
          case 105:
            this.cai_liao.dan_jia = 6000
            break
          case 128:
            this.cai_liao.dan_jia = 5900
            break
          default:
            this.cai_liao.dan_jia = 5800
            break
        }
      } else {
        let i = this.option.ke_zhong[this.name].indexOf(this.cai_liao.ke_zhong)
        this.cai_liao.dan_jia = this.option.dan_jia[this.name][i]
      }
      this.transferValue()
    },
    clearInfo () {
      this.cai_liao.ke_zhong = ''
      this.cai_liao.dan_jia = ''
    }
  }
}
</script>

<style scoped>
.content{
  margin: 10px 0;
}

.length{
  width: 100px;
  margin-top: 10px;
}
</style>
